package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.BaseServiceImpl;
import com.conf.web.FileDel;
import com.constant.PowerEnum;
import com.entity.Artical;
import com.entity.ArticalChannel;
import com.entity.Channel;
import com.exception.ServiceException;
import com.repository.ArticalRepository;
import com.repository.ChannelRepository;
import com.req.ChannelPageReq;
import com.req.ChannelReq;
import com.req.RssPageReq;
import com.res.ArticalRes;
import com.res.ChannelRes;
import com.base.BasePageRes;
import com.res.UserRes;
import com.service.ChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ChannelServiceImpl extends BaseServiceImpl<ChannelReq, ChannelRes, ChannelRepository, Channel> implements ChannelService {
    @Autowired
    private FileDel fileDel;
    @Autowired
    private ArticalServiceImpl articalService;
    @Autowired
    private ArticalChannelServiceImpl articalChannelService;
    @Autowired
    private ArticalRepository articalRepository;
    @Override
    public BasePageRes queryPage(ChannelPageReq pageReq, UserRes userRes) {
        if (StringUtils.isEmpty(pageReq.getPage())) {
            throw new ServiceException("请传入page");
        }
        if (StringUtils.isEmpty(pageReq.getSize())) {
            throw new ServiceException("请传入size");
        }


        QueryWrapper<Channel> wrapper = new QueryWrapper<>();
        PowerEnum powerEnum = PowerEnum.codeOf(userRes.getPower());
        switch (powerEnum) {
            case Admin:
                break;
            case Users:
                wrapper.lambda().and(Wrapper -> Wrapper.eq(Channel::getUserId, userRes.getId()));
                break;
            default:
                break;
        }
        wrapper.lambda().and(Wrapper->Wrapper.eq(Channel::getUserId,pageReq.getUserId()));
        if (!StringUtils.isEmpty(pageReq.getDescre())) {
            wrapper.lambda().and(Wrapper->Wrapper.like(Channel::getChannelName,pageReq.getDescre()).or().like(Channel::getDescre,pageReq.getDescre()));
        }
        wrapper.lambda().orderByDesc(Channel::getMoDate).orderByDesc(Channel::getCrDate);
        IPage<Channel> pages = new Page<>(pageReq.getPage(), pageReq.getSize());
        IPage<Channel> articalIPage = this.page(pages, wrapper);
        BasePageRes pageRes = this.convertIpage(articalIPage, new ChannelRes());

        return pageRes;
    }

    @Override
    public ChannelRes saveChannel(ChannelReq channelReq) {
        return this.handle(channelReq);
    }

    @Override
    public ChannelRes del(ChannelReq channelReq) {
        //删除图片
        Channel channel = this.queryT(channelReq);
        fileDel.del(channel.getIco());
        return super.del(channelReq);
    }

    @Override
    public ChannelRes update(ChannelReq channelReq) {
        //删除旧的图片
        Channel channel = this.queryT(channelReq);
        if(!Objects.equals(channelReq.getIco(),channel.getIco())){
            fileDel.del(channel.getIco());
        }
        return super.update(channelReq);
    }


        @Override
        public BasePageRes detailPage(RssPageReq pageReq) {
            if (StringUtils.isEmpty(pageReq.getPage())) {
                throw new ServiceException("请传入page");
            }
            if (StringUtils.isEmpty(pageReq.getSize())) {
                throw new ServiceException("请传入size");
            }
            if(StringUtils.isEmpty(pageReq.getChannelId())){
                throw new ServiceException("请传入频道Id");
            }


//            QueryWrapper<ArticalChannel> articalChannelQueryWrapper = new QueryWrapper<>();
//            articalChannelQueryWrapper.lambda().eq(ArticalChannel::getChannelId,pageReq.getChannelId());
//            List<ArticalChannel> articalChannelList = articalChannelService.list(articalChannelQueryWrapper);
//
//            if(CollectionUtils.isEmpty(articalChannelList)){
//                return new BasePageRes();
//            }

            IPage<ArticalRes> pages = new Page<>(pageReq.getPage(), pageReq.getSize());
            IPage<ArticalRes> articalResIPage = articalRepository.channelPage(pages, pageReq, null);
            BasePageRes pageRes = articalService.convertIpage(articalResIPage,  new ArticalRes());
            return pageRes;
        }

    @Override
    public BasePageRes frontqueryPage(ChannelPageReq pageReq) {
        if (StringUtils.isEmpty(pageReq.getPage())) {
            throw new ServiceException("请传入page");
        }
        if (StringUtils.isEmpty(pageReq.getSize())) {
            throw new ServiceException("请传入size");
        }


        QueryWrapper<Channel> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(pageReq.getDescre())) {
            wrapper.lambda().and(Wrapper->Wrapper.like(Channel::getChannelName,pageReq.getDescre()).or().like(Channel::getDescre,pageReq.getDescre()));
        }
        wrapper.lambda().orderByDesc(Channel::getMoDate).orderByDesc(Channel::getCrDate);
        IPage<Channel> pages = new Page<>(pageReq.getPage(), pageReq.getSize());
        IPage<Channel> articalIPage = this.page(pages, wrapper);
        BasePageRes pageRes = this.convertIpage(articalIPage, new ChannelRes());

        return pageRes;
    }
}

